package com.ruoyi.web.controller.system;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SysTopic;
import com.ruoyi.system.service.impl.SysTopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/system/topic")
public class SysTopicController extends BaseController {

    @Resource
    private SysTopicService sysTopicService;


    /**
     * 根据课程id和题目类型查询
     * @param courseId
     * @param topicType
     * @return
     */
    @GetMapping
    public TableDataInfo getTopicByCourseIdAndType(@RequestParam("courseId") String courseId, @RequestParam("topicType") String topicType){
        startPage();
        List<SysTopic> list = sysTopicService.getTopicByCourseIdAndType(courseId, topicType);
        return getDataTable(list);
    }
    /**
     * 分页条件查询
     *
     * @param topic 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo list(SysTopic topic) {
        startPage();
        List<SysTopic> list = sysTopicService.list(topic);
        return getDataTable(list);
    }

    /**
     * 增加题目
     */
    @PostMapping    //限定请求方式为Post
    public AjaxResult add(@RequestBody SysTopic topic){
        // 测试，确保topicName不为null或空字符串
        if (topic.getTopicName() == null || topic.getTopicName().trim().isEmpty()) {
            return AjaxResult.error("Topic name cannot be null or empty.");
        }
        log.info("增加题目:{}",topic);

        topic.setCreateTime(LocalDateTime.now());
        topic.setUpdateTime(LocalDateTime.now());
        topic.setCreateBy(getUsername());
        topic.setCreateBy(getUsername());

        //调用service添加题目数据
        sysTopicService.add(topic);
        return AjaxResult.success();
    }

    /**
     * 删除题目
     *
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Integer id){
        log.info("根据id删除题目:{}",id);
        //调用service删除部门
        sysTopicService.delete(id);
        return AjaxResult.success();
    }

    /**
     * 用ID查询题目信息
     *
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Integer id){
        log.info("根据ID查询题目信息, id: {}",id);
        SysTopic topic = sysTopicService.getById(id);
        return AjaxResult.success(topic);
    }

    /**
     * 更新题目信息
     *
     */
    @PutMapping
    public AjaxResult update(@RequestBody SysTopic topic){
        log.info("更新题目信息 : {}", topic);

        topic.setUpdateBy(getUsername());
        topic.setUpdateTime(LocalDateTime.now());

        sysTopicService.update(topic);
        return AjaxResult.success();
    }

    //    /**
//     * 分页查询
//     *
//     * @param topic      筛选条件
//     * @return 查询结果
//     */
//    @GetMapping("/list")
//    public TableDataInfo list(SysTopic topic) {
//        startPage();
//        List<SysTopic> topics = sysTopicService.queryList(topic);
//        return getDataTable(topics);
//    }

//    /**
//     * 通过主键查询单条数据
//     *
//     * @param id 主键
//     * @return 单条数据
//     */
//    @GetMapping(value = "/{topicId}")
//    public AjaxResult queryById(@PathVariable("topicId") Long id) {
//        return success(sysTopicService.queryById(id));
//    }


//    /**
//     * 查询题目数据
//     *
//     */
//    @GetMapping("/list")	//限定请求方式为GET
//    public AjaxResult list(){
//        log.info("查询全部题目数据");
//        //调用service查询题目数据
//        List<SysTopic> topicList = sysTopicService.list();
//        return AjaxResult.success(topicList);
//    }

}



